﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using TuKu.Load;

namespace TuKu.PPS
{
    public partial class ppsdownload : Form
    {
        List<string> list;
        public ppsdownload()
        {
            InitializeComponent();
        }

        private void ppsdownload_Load(object sender, EventArgs e)
        {
            DataBindClass();
        }

        private void DataBindClass()
        {
            string strsql = "select count(id) from urllist where cateid=11";
            txtleft.Text = Maticsoft.DBUtility.DbHelperOleDb.GetSingle(strsql).ToString();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                timer1.Enabled = true;
            }
            dodownload();
        }
        private string tranurl(string url)
        {
            //F:\Webdup\MyWebsites\tu.pps.tv\download\latest\upload
            //http://upload.ppstream.com/baike/tu/mm/2008-04/12082337091811321.jpg
            //F:\Webdup\MyWebsites\tu.pps.tv\download\latest\upload\baike\uploadimage\2008-03\18553_1.jpg
            url = url.Replace("/./", "/");
            string str = url.Replace("http://upload.ppstream.com", @"F:\Webdup\MyWebsites\tu.pps.tv\download\latest\upload");
            str = str.Replace("/", "\\");

            string path = TuKu.Common.FileManager.GetRemoveFile(str);
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch
                {

                }
            }
            return str;
        }
        private void beginDownload(string url, string localfile)
        {
            System.Net.HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
            using (Stream stream = hwr.GetResponse().GetResponseStream())
            {
                try
                {
                    Image img = Image.FromStream(stream);
                    img.Save(localfile);
                    stream.Close();
                }
                catch
                {
                    stream.Close();
                }
            }
        }



        private void dodownload()
        {
            list = new List<string>();
            textBox1.Text = "";
            btnDownload.Enabled = false;
            string count = txtCount.Text;
            string strsql = "select top " + count + " img,id from urllist where cateid=11";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            string url = string.Empty;
            string localfile = string.Empty;

            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                url = dr["img"].ToString();
                list.Add(dr["id"].ToString());
                localfile = tranurl(url);
                if (!File.Exists(localfile))
                {
                    //beginDownload(url, localfile);
                    new Thread(new ThreadStart(new myDownload(url, localfile).download)).Start();
                    label2.Text = (i++).ToString();
                    //new myDownload(url, localfile).download();
                    textBox1.Text = dr["id"].ToString() + ":" + localfile + "\r\n";
                    textBox1.Refresh();
                    label2.Refresh();
                    //while(true)
                    //{
                    //    if(File.Exists(localfile))
                    //    break;
                    //}

                }

            }

            StringBuilder sb = new StringBuilder();
            sb.Append(" update urllist set cateid=cateid*100 where id in ( ");
            foreach (string str in list)
            {
                sb.Append(str);
                sb.Append(",");
            }
            sb.Append("0)");
            int effect = Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
            //MessageBox.Show("处理" + count + "条记录,数据库更新"+effect.ToString()+"条记录");
            DataBindClass();
            btnDownload.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            dodownload();
        }


    }

    public class myDownload
    {
        private string _url;
        private string _localfile;
        public myDownload(string url, string localfile)
        {
            this._url = url;
            this._localfile = localfile;
        }

        public void download()
        {
            System.Net.HttpWebRequest hwr = null;
            System.IO.Stream stream = null;
            try
            {
                hwr = (HttpWebRequest)HttpWebRequest.Create(_url);
                stream = hwr.GetResponse().GetResponseStream();
                Image img = Image.FromStream(stream);
                img.Save(_localfile);
                stream.Close();
            }
            catch
            {
                return;
            }
            
           
        }


    }

}
